#ifndef APPCONTROLLER_H
#define APPCONTROLLER_H

#include <QObject>
#include "mqttclient.h"
#include "serialmanager.h"
#include <QDateTime>
#include <QDir>
#include <QFileInfo>
#include <QTextStream>

class AppController : public QObject
{
    Q_OBJECT

    Q_PROPERTY(MqttClient* mqtt READ mqtt CONSTANT)
    Q_PROPERTY(SerialManager* serial READ serial CONSTANT)
    Q_PROPERTY(bool mqttForward READ mqttForward WRITE setMqttForward NOTIFY mqttForwardChanged)

public:
    explicit AppController(QObject *parent = nullptr);

    // 暴露给 QML
    MqttClient* mqtt() const { return _mqtt; }
    SerialManager* serial() const { return _serial; }

    bool mqttForward() const { return _mqttForward; }
    void setMqttForward(bool enabled);

signals:
    void mqttForwardChanged();

private slots:
    // void handleMqttMessage();
    void handleForwardPrepared(const QByteArray &forwardMsg);  // 👈 槽函数
    void stopLogging(void);

private:
    MqttClient *_mqtt = nullptr;
    SerialManager *_serial = nullptr;
    bool _mqttForward { true };

    QFile *_csvFile = nullptr;      // ✅ 新增
    QTextStream _logStream;
    QTextStream _csvStream;         // ✅ 新增
    bool _isRecording = false;

    void startLogging();
    // void stopLogging();
    void writeLogLine(const QString &line);

};

#endif // APPCONTROLLER_H
